package ar.com.jwa.domain.emailing;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import ar.com.jwa.domain.User;

@SuppressWarnings("restriction")
public class ConfirmationHtmlMail extends ConfirmationMail{

	
	/**
	 * Este constructor es usado por spring para crear una unica instancia de la clase Mail y ser distribuida por el sistema
	 */
	public ConfirmationHtmlMail(String host, String from,String subject,String protocol, String smtpUser, String smtpPassword) {
		super();
		this.props = System.getProperties();
		this.session = Session.getDefaultInstance(props, null);
		this.message = new MimeMessage(session);
		this.messageBodyPart = new MimeBodyPart();
		this.multipart = new MimeMultipart("related");
		this.host = host;
		this.from = from;
		this.subject = subject;
		this.protocol = protocol;
		this.smtpUser = smtpUser;
		this.smtpPassword = smtpPassword;
	}
	
	public ConfirmationHtmlMail(){
		super();
	}
	
	@Override
	public StringBuffer buildMail(){
		
		StringBuffer htmlMail = new StringBuffer();
		
		buildMailHeader(htmlMail);
		buildListPlayers(htmlMail, this.getConfirmedPlayers(),"green","Lista Confirmados");
		buildListPlayers(htmlMail, this.getRefusedPlayers(),"red","Lista Ausencias");
		buildListPlayers(htmlMail, this.getInvitedPlayers(),"gray","No idea");
		buildMailFooter(htmlMail);
		
		return htmlMail;
	}
	
	private void buildMailHeader(StringBuffer htmlMail){
		
		htmlMail.append("<table align='center' border='2'>");
		htmlMail.append("	<tr>");
		htmlMail.append("		<td>");
		htmlMail.append("			<table align='center'>");
		htmlMail.append("				<tr>");
		htmlMail.append("					<td>");
		htmlMail.append("  	     			    <img alt='Vamo la plaza' src=\"cid:soccer\">");
		htmlMail.append("					</td>");
		htmlMail.append("				</tr>");
		htmlMail.append("				<tr align='center'>");
		htmlMail.append("					<td>");
		htmlMail.append("						<h3> Soccer Cancha 11 </h3>");
		htmlMail.append("						<h5> 11 de octubre </h5>");
		htmlMail.append("						<h5> 23 hs </h5>");
		htmlMail.append("						<h5> saraza 1123 </h5>");
		htmlMail.append("					</td>");
		htmlMail.append("				</tr>");
		htmlMail.append("			</table>");	
		htmlMail.append("			<table align='center'>");
		htmlMail.append("				<tr>");
	}
	
	private void buildListPlayers(StringBuffer htmlMail,Collection<User> playersList,String color,String tittle){
		Iterator<User> it = playersList.iterator();
		int playerNumber = 0;
		
		htmlMail.append("					<td align='left' style='padding-right: 30px'>");
		htmlMail.append("						<table>");
		htmlMail.append("							<tr>");
		htmlMail.append("								<td>");
		htmlMail.append("									<p style='font: bold;'>"+tittle+"</p>");
		htmlMail.append("								</td>");
		htmlMail.append("							</tr>");
		htmlMail.append("						</table>");
		htmlMail.append("						<table border='1'>");
		
		while (it.hasNext()){
			
			playerNumber++;
			
			User user = it.next();
			
			htmlMail.append("						<tr>");
			htmlMail.append("							<td width='50'>");
			htmlMail.append("								<p>");
			htmlMail.append(									playerNumber + " -  ");
			htmlMail.append("							    </p>");
			htmlMail.append("								<td/>");
			htmlMail.append("								<td width='100'>");
			htmlMail.append("									<p style='color:"+ color+"'align='center'>");
			htmlMail.append(										user.getNickName());
			htmlMail.append("									</p>");
			htmlMail.append("								<td/>");
			htmlMail.append("						</tr>");
		}
		htmlMail.append("						</table>");
		htmlMail.append("					</td>");
		
	}
	
	private void buildMailFooter(StringBuffer htmlMail){
		htmlMail.append("				</tr>");
		
		if (! this.getConfirmedPlayers().contains(this.getUser()) ){
			htmlMail.append("			<tr>");
			htmlMail.append("				<td>");
			htmlMail.append("					<p>");
			htmlMail.append("						Aun no confirmaste! para hacerlo presiona <a href=''>aquì</a>");
			htmlMail.append("					<p>");
			htmlMail.append("				</td>");
			htmlMail.append("			</tr>");
		}
		htmlMail.append("			</table>");
		htmlMail.append("		</td>");
		htmlMail.append("	</tr>");
		htmlMail.append("</table>");
	}
	
	public void prepareMail() throws MessagingException, IOException {

		String file = "/home/ngonzalez/Desarrollo/Seminario/LastWorkSpace/ComunidadGarmendia/WebContent/images/soccer.jpg";
		
		String mailText = buildMail().toString();
		
		this.messageBodyPart.setContent(mailText, "text/html");
		this.multipart.addBodyPart(this.getMessageBodyPart());
		this.messageBodyPart = new MimeBodyPart();
		
		DataSource fds = new FileDataSource(file);
		
		this.messageBodyPart.setDataHandler(new DataHandler(fds));
		this.messageBodyPart.setHeader("Content-ID", "soccer");
		this.message.setContent(this.getMultipart());
		this.message.setContent(mailText,"text/html");
		this.multipart.addBodyPart(messageBodyPart);
		this.message.setContent(multipart);
	}

}
